package com.wgl.va.common;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class ServerInfo {
	private static Logger logger = Logger.getLogger(ServerInfo.class);
	private static String USER_NAME = "nick00123";
	private static String PASSWORD = "xiaobainick1";
	private static String PASSWORD_MD5 = "505d8a0fc44f229fae00a4fb993af972";

	public static void main(String[] args) throws IOException {
		showServerInfo();
	}

	/**
	 * 获取服务器硬盘信息
	 * 
	 * @param serverId
	 * @return
	 * @throws IOException
	 */
	public static String getServerDiskInfo(String serverId) throws IOException {
		Map<String, String> params = new HashMap<String, String>();
		String loginUrl = "http://idcys.com/hostadm/login.asp";
		String serverListUrl = "http://idcys.com/hostadm/selfhostdiskq.asp";
		params.put("username", USER_NAME);
		params.put("password", PASSWORD_MD5);
		params.put("id", serverId);
		params.put("go", "a");
		params.put("returl", "http://www.idcen.net");
		Response controlResponse = Jsoup.connect(loginUrl).data(params).method(Method.POST).timeout(5000).execute();
		Map<String, String> map = controlResponse.cookies();
		Map<String, String> nerCon = new HashMap<String, String>();
		nerCon.put("id", serverId);
		String body = NetTool.getDocByFakeBrowser(serverListUrl, 5000, 2, params, map).toString();
		Map<String, String> result = new HashMap<String, String>();
		String percent = Tool.findStrByRegEx(body, "(?<=\\s)\\d{1,3}%");
		// System.out.println(body);
		// String used = Tool.findStrByRegEx(body, "(?<=:)\\d{1,6}\\sM");
		// String detail = Tool.findStrByRegEx(body, ":.*M.*\\d{1,6}\\sM");
		// detail = "已使用" + detail.replace("<br>", " ");
		// result.put("percent", percent);
		// result.put("max", percent);
		return percent;
	}

	/**
	 * 获取服务器流量信息
	 * 
	 * @param serverId
	 * @return
	 * @throws IOException
	 */
	public static String getServerFlowInfo(String serverId) throws IOException {
		Map<String, String> params = new HashMap<String, String>();
		String loginUrl = "http://idcys.com/hostadm/login.asp";
		String serverListUrl = "http://idcys.com/hostadm/log.asp";
		params.put("username", USER_NAME);
		params.put("password", PASSWORD_MD5);
		params.put("id", serverId);
		params.put("go", "a");
		params.put("returl", "http://www.idcen.net");
		Response controlResponse = Jsoup.connect(loginUrl).data(params).method(Method.POST).timeout(5000).execute();
		Map<String, String> map = controlResponse.cookies();
		Map<String, String> nerCon = new HashMap<String, String>();
		nerCon.put("id", serverId);
		String body = NetTool.getDocByFakeBrowser(serverListUrl, 5000, 2, params, map).toString();
		String percent = Tool.findStrByRegEx(body, "(?<=>)\\d{1,3}%");
		return percent;
	}

	/**
	 * 获取服务器数据库信息
	 * 
	 * @param serverId
	 * @return
	 * @throws IOException
	 */
	public static String getSqlInfo(String serverId) throws IOException {
		Map<String, String> params = new HashMap<String, String>();
		String loginUrl = "http://idcys.com/hostadm/login.asp";
		String serverListUrl = "http://idcys.com/hostadm/sqla.asp";
		params.put("username", USER_NAME);
		params.put("password", PASSWORD_MD5);
		params.put("id", serverId);
		params.put("go", "a");
		params.put("returl", "http://www.idcen.net");
		Response controlResponse = Jsoup.connect(loginUrl).data(params).method(Method.POST).timeout(5000).execute();
		Map<String, String> map = controlResponse.cookies();
		Map<String, String> nerCon = new HashMap<String, String>();
		nerCon.put("id", serverId);
		String body = NetTool.getDocByFakeBrowser(serverListUrl, 5000, 2, params, map).toString();
		String used = Tool.findStrByRegEx(body, "(?<=>)\\d{1,4}(?=M<)");
		if (!Tool.validStr(used)) {
			return "无";
		}
		String total = Tool.findStrByRegEx(body, "(?<=:\\s)\\d{1,4}(?=M)");
		int usedInt = "0".equals(used) ? 0 : Integer.valueOf(used);
		int totalInt = Integer.valueOf(total);
		return (int) (usedInt * 100 / totalInt) + "%";
	}

	/**
	 * 获取用户的服务器id
	 * 
	 * @param serverId
	 * @return
	 * @throws IOException
	 */
	public static List<String> getServerIdListInfo() throws IOException {
		Map<String, String> params = new HashMap<String, String>();
		String loginUrl = "http://www.idcen.net/user/userlogin.asp";
		String serverListUrl = "http://www.idcen.net/user/hostadm.asp";
		// http://www.idcen.net/user/hostadm2.asp?id=70726&go=a
		params.put("username", USER_NAME);
		params.put("password", PASSWORD);
		Response controlResponse = Jsoup.connect(loginUrl).data(params).method(Method.POST).timeout(5000).execute();
		Map<String, String> map = controlResponse.cookies();
		List<String> serverList = new ArrayList<String>();
		Document doc = NetTool.getDocByFakeBrowser(serverListUrl, 5000, 2, params, map);
		Elements elements = doc.getElementsByClass("cssidcena");
		for (Element element : elements) {
			String href = element.attr("href");
			if (href.contains("go=a")) {
				String id = Tool.findStrByRegEx(href, "(?<=id=)\\d{1,12}(?=&)");
				serverList.add(id);
			}
		}
		return serverList;
	}

	public static void showServerInfo() throws IOException {
		List<String> serverList = getServerIdListInfo();
		for (String id : serverList) {
			String disk = getServerDiskInfo(id);
			String sql = getSqlInfo(id);
			String flow = getServerFlowInfo(id);
			logger.info("服务器id:" + id + ",硬盘使用情况:" + disk + ",流量使用情况:" + flow + ",数据库使用情况:" + sql);
//			System.out.println("服务器id:" + id + ",硬盘使用情况:" + disk + ",流量使用情况:" + flow + ",数据库使用情况:" + sql);

		}
	}

}
